#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/11/21 11:00
@desc
"""

# 淘宝商品信息
# 引入 selenium 中的webdriver
# 1.创建firefox浏览器对象
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

from com.teradata.seleniumtest.loginTaobao import login_in

class TB(object):
    def __init__(self, keyword, driver):
        self.keyword = keyword
        self.driver = driver

    def start_taobao(self):
        # 创建浏览器对象
        # chrome_options = webdriver.ChromeOptions()
        # chrome_options.add_argument("--start-maximized")
        #
        # driver = webdriver.Chrome(
        #     chrome_options=chrome_options
        # )
        # 打开淘宝网址
        self.driver.get('http://www.taobao.com')
        # 通过id找到输入框
        search_input = self.driver.find_element_by_id('q')
        # 输入要搜索的关键词
        search_input.send_keys(self.keyword)
        # 利用class来找到搜索按钮
        search_btn = self.driver.find_element_by_class_name('btn-search')
        # 点击搜索按钮
        search_btn.click()
        time.sleep(1)
        # 打开文件
        file_handle = open('%s.txt' % self.keyword, 'w', encoding='utf-8')
        for x in range(1, 2):
            print('正在获取第%s页数据，请稍后。。。。' % x)
            # for 循环执行五次
            for x in range(1, 11, 2):
                time.sleep(1)
                # 把x换算成小数
                j = x / 10
                # 拼接让浏览器滚动单位js代码
                js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
                self.driver.execute_script(js)

            # 查找当前页所有的商品信息,返回一个列表
            # 电脑 鼠标 键盘。。。class_name('info-cont')
            # 羽绒服  class-name('J_ClickStat')
            shops = self.driver.find_elements_by_class_name('info-cont')

            if len(shops) == 0:
                shops = self.driver.find_elements_by_class_name('J_MouserOnverReq')
            # for循环输出每一个信息
            for shop in shops:
                # 写入文件
                file_handle.write(shop.text)
                file_handle.write('\n')
            # 找到下一页，点击
            # nextpage = self.driver.find_element_by_link_text('下一页')
            # nextpage.click()
            next_li = self.driver.find_element_by_class_name('next')
            next_li.click()
        # 关闭文件
        file_handle.close()
        # 退出浏览器
        # self.driver.quit()


# 输出
print(__name__)
# __name__  值为__main__ 表示是从当前文件直接运行
# __name__ 值为 当前文件名称 表示别的文件引用执行的
# 如果是从当前文件直接运行的，执行以下代码
if __name__ == '__main__':
    keyword = input('请输入查询的关键词：')

    DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.loadImages'] = True
    DesiredCapabilities.PHANTOMJS[
        'phantomjs.page.settings.userAgent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 "

    driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
    driver.set_script_timeout(30)
    driver.set_page_load_timeout(30)
    user = 'yz271544'
    password = 'ShiHuiTao332324'
    login_in(driver, user, password)
    print('------- start scraw --------')
    tb = TB(keyword, driver)
    tb.start_taobao()

